//
//  Extension.swift
//  SmartHome
//
//  Created by Jekin on 2018/12/24.
//  Copyright © 2018 liumaoqiang. All rights reserved.
//

import Foundation
import RxCocoa
import RxSwift
var disposeBagKey = 100
extension NSObject {
    var disposeBag : DisposeBag {
        set {
            objc_setAssociatedObject(self, &disposeBagKey, newValue, objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN_NONATOMIC)
        }
        get {
            if let result = objc_getAssociatedObject(self, &disposeBagKey) as? DisposeBag {
                return result
            }
            let dispose = DisposeBag()
            objc_setAssociatedObject(self, &disposeBagKey, dispose, objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN_NONATOMIC)
            return dispose
        }
    }
    
}

extension String {
    //有效邮箱
    func validateEmail() -> Bool {
        if self.isBlank() {
            return false
        }
        let Regex = "[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}"
        let predicate = NSPredicate(format: "SELF MATCHES %@", Regex)
        return predicate.evaluate(with:self)
    }
    //有效手机号
    func validatePhone() -> Bool {
        if self.isBlank() {
            return false
        }
        
        let mobile = "^1([3-9][0-9])\\d{8}$"
        let cm = "^1([3-9][0-9])\\d{8}$"
        let cu = "^1([3-9][0-9])\\d{8}$"
        let ct = "^1([3-9][0-9])\\d{8}$"
        
        let mobileRegex = NSPredicate(format: "SELF MATCHES %@", mobile)
        let cmRegex = NSPredicate(format: "SELF MATCHES %@", cm)
        let cuRegex = NSPredicate(format: "SELF MATCHES %@", cu)
        let ctRegex = NSPredicate(format: "SELF MATCHES %@", ct)
        let result : Bool = mobileRegex.evaluate(with: self)||cmRegex.evaluate(with: self)||cuRegex.evaluate(with: self)||ctRegex.evaluate(with: self)
        return result
    }
    //字符串判空
    func isBlank() -> Bool {
        if self.count == 0 {
            return false
        }
        
        let set = NSCharacterSet.whitespaces
        let trimmedStr = self.trimmingCharacters(in: set)
        if trimmedStr.count == 0 {
            return false
        }
        return true
    }
}


@inline(__always) func getImage(imageName:String)-> UIImage? {
    return UIImage.init(named:imageName)
}

@inline(__always) func getKeyWindow()-> UIWindow? {
    return UIApplication.shared.keyWindow
}

@inline(__always) func tabBarHeight() -> CGFloat {
    if #available(iOS 11.0, *) {
        return CGFloat(49.0 + (UIApplication.shared.keyWindow?.safeAreaInsets.bottom ?? 0.0))
    }
    return 49.0
    
}

@inline(__always) func navHeight() -> CGFloat {
    if #available(iOS 11.0, *) {
        var top = UIApplication.shared.keyWindow?.safeAreaInsets.top ?? 0
        top = top > 0 ? top : 20
        return 44.0 + CGFloat(top)
    }
    return 64.0
    
}
@inline(__always) func hasIphoneSafe() -> Bool {
    if #available(iOS 11.0, *) {
        return Int (UIApplication.shared.keyWindow?.safeAreaInsets.bottom ?? 0) > 0
    }
    return false
}



@inline(__always) func RGBA(R:Double, G:Double ,B:Double, A:Double) -> UIColor {
    return UIColor.init(red: CGFloat(Double((R) / 255.0)), green: CGFloat(Double(G/255.0)), blue: CGFloat(Double(B/255.0)), alpha: CGFloat(Double(A)))
}

@inline(__always) func RGB(R:Double, G:Double ,B:Double) -> UIColor {
    return RGBA(R: R, G: G, B: B, A: 1)
}


@inline(__always) func APPVersion() -> String {
    return Bundle.main.infoDictionary!["CFBundleShortVersionString"] as! String
}
